#include "promotion.h"

Promotion::Promotion(GameInfo& gameInfo) : _gameInfo(gameInfo),
    _gui(*new GuiSystem()), _view(*new PromotionView(*this, _gui))
{
    //ctor
}

Promotion::~Promotion()
{
    delete &_gui;
    delete &_view;
}

bool Promotion::update(float dt, const Input& input)
{
    GuiId button;
    if (_gui.interact(input, &button, (int*)NULL)) {
        switch (button) {
            case GUI_QUEEN:
                _gameInfo.setPromotion(QUEEN);
                return false;
            case GUI_ROOK:
                _gameInfo.setPromotion(ROOK);
                return false;
            case GUI_BISHOP:
                _gameInfo.setPromotion(BISHOP);
                return false;
            case GUI_KNIGHT:
                _gameInfo.setPromotion(KNIGHT);
                return false;
            default:
                break;
        }
    }

    return true;
}

IGameStateView& Promotion::getView()
{
    return _view;
}

void Promotion::onEnter()
{

}

PromotionView::PromotionView(Promotion& model, GuiSystem& gui) : _model(model),
    _gui(gui)
{
    _gui.addStatic(0, 0.35f, 0.2f, 0.3f, 0.6f,
        *new Texture("..\\..\\models\\textures\\prom_bg.png"));
    _gui.addStatic(1, 0.38f, 0.68f, 0.25f, 0.12f,
        *new Texture("..\\..\\models\\textures\\prom_title.png", true));

    _gui.addButton(GUI_QUEEN, 0.4f, 0.55f, 0.2f, 0.07f,
        *new Texture("..\\..\\models\\textures\\queen_button.png", true));
    _gui.addButton(GUI_ROOK, 0.4f, 0.45f, 0.2f, 0.07f,
        *new Texture("..\\..\\models\\textures\\rook_button.png", true));
    _gui.addButton(GUI_BISHOP, 0.4f, 0.35f, 0.2f, 0.07f,
        *new Texture("..\\..\\models\\textures\\bishop_button.png", true));
    _gui.addButton(GUI_KNIGHT, 0.4f, 0.25f, 0.2f, 0.07f,
        *new Texture("..\\..\\models\\textures\\knight_button.png", true));
}

void PromotionView::draw(bool clear)
{
    setup2dView();

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glColor4f(0.5, 0.5f, 0.5f, 0.5f);
    glBegin(GL_QUADS);
        glVertex2f(0.0f, 0.0f);
        glVertex2f(1.0f, 0.0f);
        glVertex2f(1.0f, 1.0f);
        glVertex2f(0.0f, 1.0f);
    glEnd();

    glDisable(GL_BLEND);

    _gui.draw();
}

void PromotionView::onEnter()
{

}

void PromotionView::setup2dView()
{
    glDisable(GL_CULL_FACE);
    glDisable(GL_LIGHTING);
    glDisable(GL_DEPTH_TEST);
    //glEnable(GL_TEXTURE_2D);

    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}
